package com.jzy.gulimall.product.controller;

import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.R;
import com.jzy.gulimall.product.entity.AttrEntity;
import com.jzy.gulimall.product.entity.AttrGroupEntity;
import com.jzy.gulimall.product.service.AttrAttrgroupRelationService;
import com.jzy.gulimall.product.service.AttrGroupService;
import com.jzy.gulimall.product.service.AttrService;
import com.jzy.gulimall.product.service.CategoryService;
import com.jzy.gulimall.product.vo.AttrGroupRelationVo;
import com.jzy.gulimall.product.vo.AttrGroupWithAttrVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author jzy
 * @email 1802511725@qq.com
 * @date 2020-07-11 10:08:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 发布商品 规格参数
     * 根据分类Id查出对应的分组 以及分组的所有属性
     * product/attrgroup/225/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrVo> vos = attrGroupService.getAttrGroupAndAttrByCatId(catelogId);
        return R.ok().put("data", vos);
    }


    /**
     * 属性分组 关联 新建关联 新增  attrgroup和attr关联关系
     * product/attrgroup/attr/relation
     */
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatchRelation(vos);
        return R.ok();
    }

    /**
     * 属性分组 关联 新建关联 列表 获取当前分组没有关联的所有属性
     * product/attrgroup/2/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params, @PathVariable() Long attrgroupId) {
        PageUtils list = attrService.getAttrNoRelationList(params, attrgroupId);
        return R.ok().put("page", list);
    }

    /**
     * 属性分组 关联 删除 attrgroup和attr关联关系
     * product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 属性分组 关联 列表 分组关联的规格参数 列表查询
     * /product/attrgroup/{attrgroupId}/attr/relation
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> list = attrService.getAttrRelationList(attrgroupId);
        return R.ok().put("data", list);
    }

    /**
     * 属性分组 列表  查询 分页查询 模糊匹配
     * catelogId为0则查询所有,否则根据catelogId条件查询
     */
    @RequestMapping("/list/{catelogId}")
    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 属性分组 回显 信息
     * 额外封装catelogId三级分类路径
     */
    @RequestMapping("/info/{attrGroupId}")
    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 属性分组 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 属性分组 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 属性分组 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        // TODO: 2020/9/8 删除属性分组时,检查属性分组关联的属性,同时删除中间表信息
        return R.ok();
    }

}
